<?php

namespace Api\Controller;

class UserinfoController extends ApiController {

    private $token;
    private $deviceid;
    private $user_id;
    private $userinfo;
    private $error_info;

    function __construct() {
        parent::__construct();
        $this->token = I('request.token');
        $this->deviceid = I('request.deviceid');
        $this->userinfo = $this->verify_token($this->token, $this->deviceid);
        $this->user_id = $this->userinfo['user_id'];
        $this->error_info = L('error_info');
        $this->uinfo = array(
            'user_fname' => '',
            'user_lname' => '',
            'user_aname' => '',
            'user_gender' => 0,
            'user_tel' => '');
    }

    public function getinfo() {
        //基本信息验证
        //数据对比        
        $User = D('Users');
        $user = $User->findUserbyId($this->user_id);
        if (empty($user['username'])) {
            $this->error($this->error_info['oserror']);
        }

        $userinfo = D('Users')->getUserinfobyuserid($this->user_id);
        $arrInfo['user'] = array();
        if ($userinfo) {
            foreach ($userinfo as $key => $value) {
                if (is_null($value)) {
                    $userinfo[$key] = 0;
                }
            }
            $arrInfo['user'] = $userinfo;
        }
        $arrInfo['classes'] = D('Member')->getClasslistbyuid($this->user_id);
        $this->success(__FUNCTION__, $arrInfo);
    }

    public function upinfo() {
        $upuser = array();
        $upinfo = array();
        $data['user_fname'] = I('request.fname');
        $data['user_lname'] = I('request.lname');
        $data['user_gender'] = I('request.gender') != FALSE ? I('request.gender') : 0;
        $data['user_address'] = I('request.address');
        $data['user_tel'] = I('request.user_tel');
        $udata['nickname'] = I('request.nickname');
        $Uinfo = D('Userinfo');
        $user = D('Users')->findUserbyId($this->user_id);
        $uinfo = $Uinfo->getUinfobyuid($this->user_id);
        if (!$uinfo['user_id']) {
            $Uinfo->addUserinfo($this->user_id);
            $uinfo = $this->uinfo;
        }
        foreach ($data as $key => $value) {
            if (!empty($value) && ($data[$key] != $uinfo[$key]) && ($key != 'user_gender')) {
                $upinfo[$key] = $value;
            }
            if (($key == 'user_gender') && ($data[$key] != $uinfo[$key]) && in_array($value, array('0', '1', '2'))) {
                $upinfo[$key] = $value;
            }
        }
        if ($upinfo['user_fname']) {
            if ($upinfo['user_lname']) {
                $upinfo['user_aname'] = $upinfo['user_lname'] . $upinfo['user_fname'];
            } else {
                $upinfo['user_aname'] = $uinfo['user_lname'] . $upinfo['user_fname'];
            }
        } elseif ($upinfo['user_lname']) {
            $upinfo['user_aname'] = $upinfo['user_lname'] . $uinfo['user_fname'];
        }
        foreach ($udata as $key => $value) {
            if (!empty($value) && ($udata[$key] != $user[$key])) {
                $upuser[$key] = $value;
            }
        }
        if (count($upinfo) > 0) {
            $Uinfo->upUserinfo($this->user_id, $upinfo);
        }

        if (count($upuser) > 0) {
            D('Users')->upUser($this->user_id, $upuser);
        }
        $arrInfo = D('Users')->getUserinfobyuserid($this->user_id);
        $this->success(__FUNCTION__, $arrInfo);
    }
    
    public function changemail() {
        $aError = array(
            'noemail' => '邮箱格式不正确',
            'sendemail' => '旧邮箱已验证，已发邮件给新邮箱，可以通过链接修改',
            'oserror' => '系统错误',
            'same' => '邮箱相同',
            'success' => '邮箱修改成功'
        );
        $sEmail = I('request.email');
        if (!preg_match('/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/', $sEmail)) {
            $this->error($aError['noemail']);
        }
        $user = D('Users')->findUserbyId($this->user_id);
        if ($user['email'] && ($user['emailstatus'] == 1)) {
            $aCode = array(
                $user['username'],
                $sEmail,
                substr(md5(date('d', time()) . md5($user['username'] . $sEmail)), 2, 10)
            );
            $code = base64_encode(implode('|', $aCode));
            $sUrl = C('URL_EMAIL') . __FUNCTION__ . '?code=' . $code;
            $this->assign('url', $sUrl);
            $title = '修改邮件';
            $content = $this->fetch('Email:change');
            if (sendMail($sEmail, $title, $content)) {
                $arrInfo = array('info' => $aError['sendemail']);
                $this->success(__FUNCTION__, $arrInfo);
            } else {
                $this->error($aError['oserror']);
            }
        }
        if ($user['email'] == $sEmail) {
            $this->error($aError['same']);
        }
        $updata = array('email' => $sEmail);
        if (D('Users')->upUser($this->user_id, $updata)) {
            $arrInfo = array('info' => $aError['success']);
            $this->success(__FUNCTION__, $arrInfo);
        } else {
            $this->error($aError['oserror']);
        }
    }

    public function verifyemail() {
        $aError = array(
            'noemail' => '还没有填写邮件',
            'already' => '邮箱已验证',
            'oserror' => '系统错误',
            'same' => '邮箱相同',
            'success' => '发送邮件成功，请注意查收'
        );
        $user = D('Users')->findUserbyId($this->user_id);
        if (!$user['email']) {
            $this->error($aError['noemail']);
        }
        if ($user['emailstatus']) {
            $this->error($aError['already']);
        }
        $aCode = array(
            $user['username'],
            $user['email'],
            substr(md5(date('d', time()) . md5($user['username'] . $user['email'])), 2, 10)
        );
        $code = base64_encode(implode('|', $aCode));
        $sUrl = C('URL_EMAIL') . __FUNCTION__ . '?code=' . $code;
        $this->assign('url', $sUrl);
        $title = '验证邮件';
        $content = $this->fetch('Email:verify');
        if (sendMail($user['email'], $title, $content)) {
            $arrInfo = array('info' => $aError['success']);
            $this->success(__FUNCTION__, $arrInfo);
        } else {
            $this->error($aError['oserror']);
        }
    }

}
